/*
 	注册
 * */
define(['../../common/service', '../../model/countryList', '../../model/timezoneList'],
function( http,  countryList, timezoneList) {
    //配置I18N
    var sl = window.localStorage.getItem('language') || 'zh-cn';
    I18N.setLanguage(sl);
    var myForm = $('#signUpForm');
    var countryOptions = "";

    countryList = _.sortBy(countryList, function (item) {
        return item.name.toLowerCase();
    });

    $.each(countryList, function (i, n) {
        countryOptions += "<option value=" + n.country_code + ">" + n.name + "</option>";
    });
    var countryDropdownList = myForm.find("#country");
    countryDropdownList.append(countryOptions);
    var timezoneOptions = "";
    $.each(timezoneList, function (i, n) {
        timezoneOptions += "<option value=" + n.key + ">" + n.name + "</option>";
    });
    var timezoneDropdownList = myForm.find("#timezone");
    timezoneDropdownList.append(timezoneOptions);

        var isUserNameValid = true;
    var isEmailValid = true;
    var isInvitationCodeValid = true;

    // 绑定用户名称和邮箱失去焦点时的校验事件
    myForm.find("#username").bind("blur", function () {
        var param = {
            username: myForm.find("#username").val().trim()
        };
        http.work('/login/find',param,'POST').then(function(res){
            if(res.flag == 'success'){
                var data = res.data;
                Kit.alert_info('用户名已被注册');
                isUserNameValid = false;
            }else{
                isUserNameValid = true;
            }
        });
    });



    myForm.find("#email").bind("blur", function () {
        var param = {
            email: myForm.find("#email").val().trim()
        };
        http.work('/login/find',param,'POST').then(function(res){
            if(res.flag == 'success'){
                var data = res.data;
                Kit.alert_info(I18N.getText('user','emailhas'));
                isEmailValid = false;
            }else{
                isEmailValid = true;
            }
        });
    });


    // 绑定邀请码失去焦点时的校验事件
    myForm.find("#invitationCode").bind("blur", function () {
        var param = {
            invitationCode: myForm.find("#invitationCode").val().trim()
        };
        http.work('/login/validateInvitationCode',param,'POST').then(function(res){
            if(res.flag == 'success'){
                isInvitationCodeValid = true;
            }else{
                Kit.alert_info(I18N.getText('user','invitationCodeTtl'));
                isInvitationCodeValid = false;
            }
        });
    });

    // 绑定提交事件
    var submitForm = myForm.find('.submitForm');
    submitForm.unbind('click');
    submitForm.click(function () {
        var username = myForm.find("#username").val().trim();
        var email = myForm.find("#email").val().trim();
        var password = myForm.find("#password").val().trim();
        var confirmPassword = myForm.find("#confirmPassword").val().trim();
        var nickname = myForm.find("#nickname").val().trim();
        var invitationCode = myForm.find('#invitationCode').val().trim();
        //var company = myForm.find("#company").val().trim();
        //var website = myForm.find("#website").val().trim();
        var country = myForm.find("#country").val().trim();
        var timezoneId = myForm.find("#timezone").val().trim();
        var timezoneName = $('#timezone option:selected').text().trim();
        var language = $('#language').val();
        if ('' == username) {
            Kit.alert_info(I18N.getText('user','enterName'));
            myForm.find("#username").focus();
            return false;
        } else {
            if (!isUserNameValid) {
                Kit.alert_info(I18N.getText('user','usernamehas'));
                return false;
            }
        }
        if (!/^[a-zA-Z0-9_]{3,16}$/.test(username)) {
            Kit.alert_info(I18N.getText('user','usernameError'));
            myForm.find("#username").focus();
            return false;
        }
        if ('' == email) {
            Kit.alert_info(I18N.getText('user','enteremail'));
            myForm.find("#email").focus();
            return false;
        } else {
            if (!isEmailValid) {
                Kit.alert_info(I18N.getText('user','emailhas'));
                return false;
            } else {
                var strRegex = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
                var reg = new RegExp(strRegex);
                if (!reg.test(email)) {
                    Kit.alert_info(I18N.getText('user','emailTypeError'));
                    return false;
                }
            }
        }
        if ('' == password) {
            Kit.alert_info(I18N.getText('user','enterPassword'));
            myForm.find("#password").focus();
            return false;
        }
        if ('' == confirmPassword) {
            Kit.alert_info(I18N.getText('user','enterPasswordAgain'));
            myForm.find("#confirmPassword").focus();
            return false;
        }
        if (password.length < 6 || confirmPassword.length < 6) {
            Kit.alert_info(I18N.getText('user','passwordmin'));
            return false;
        }
        if (password != confirmPassword) {
            Kit.alert_info(I18N.getText('user','enterPasswordDiff'));
            myForm.find("#password").focus();
            return false;
        }
        if ('' == nickname) {
            Kit.alert_info(I18N.getText('user','enternickname'));
            myForm.find("#nickname").focus();
            return false;
        }
        if ('' == invitationCode){
            Kit.alert_info(I18N.getText('user','enterinvitationCode'));
            myForm.find("#invitationCode").focus();
            return false;
        }else{
            if (!isInvitationCodeValid) {
                Kit.alert_info(I18N.getText('user','invitationCodeTtl'));
                return false;
            }
        }

        if ('' == country) {
            Kit.alert_info(I18N.getText('user','selectCountry'));
            myForm.find("#country").focus();
            return false;
        }
        if ('' == timezoneId) {
            Kit.alert_info(I18N.getText('user','selectTimezone'));
            myForm.find("#timezone").focus();
            return false;
        }
        var param = {
            username: username,
            email: email,
            password: password,
            nickname: nickname,
            invitationCode:invitationCode,
            country: country,
            timezoneId: timezoneId,
            timezoneName: timezoneName,
            language:language
        };

        var loadingArea = $('body');
        Kit.loading.show(loadingArea);
        http.work('/login/register',param,'POST').then(function(res){
            if(res.flag == 'success'){
                var data = res.data;
                window.location.href = "login.html";
            }else{
                Kit.alert2(res.msg || I18N.getText('user','registerFail'));
            }
            Kit.loading.hide(loadingArea);
        });
    });
})
